package com.hustzp.com.xichuangzhu.dao;

import android.content.Context;
import android.text.TextUtils;

import com.hustzp.com.xichuangzhu.poetry.model.Works;
import com.hustzp.com.xichuangzhu.utils.DBHelper;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.util.List;
import java.util.concurrent.Callable;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/27 21:47
 * @Description:
 */
public class WorksDao {
    private Context context;
    private DBHelper helper;

    public WorksDao(Context context) {
        this.context = context;
        try {
            helper = DBHelper.getInstance(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Works getWorksById(int worksId) {
        try {
            QueryBuilder<Works, Integer> builder = (QueryBuilder<Works, Integer>) helper.getDao(Works.class).queryBuilder();
            builder.where().eq("id", worksId);
            List<Works> works = helper.getDao(Works.class).query(builder.prepare());
            return works.get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Works> getWorks(String search) {
        try {
            QueryBuilder<Works, Integer> builder = (QueryBuilder<Works, Integer>) helper.getDao(Works.class).queryBuilder();
            if (!TextUtils.isEmpty(search)) {
                builder.where().like("title", "%" + search + "%").or().like("content", "%" + search + "%");
            }
            builder.orderBy("show_order", true);

            List<Works> reviews = helper.getDao(Works.class).query(builder.prepare());
            return reviews;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void reSortWorks() {
        try {
            QueryBuilder<Works, Integer> builder = (QueryBuilder<Works, Integer>) helper.getDao(Works.class).queryBuilder();
            builder.orderByRaw("RANDOM()");

            final List<Works> reviews = helper.getDao(Works.class).query(builder.prepare());
            TransactionManager.callInTransaction(helper.getConnectionSource(), new Callable<Void>() {


                @Override
                public Void call() throws Exception {
                    int index = 0;
                    for (Works works : reviews) {
                        UpdateBuilder updateBuilder = helper.getDao(Works.class).updateBuilder();
                        updateBuilder.where().eq("id", works.getId());
                        updateBuilder.updateColumnValue("show_order", index);
                        updateBuilder.update();
                        index++;
                    }
                    return null;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Works> getLikeWorks(List<Integer> workIds) {
        try {
            QueryBuilder<Works, Integer> builder = (QueryBuilder<Works, Integer>) helper.getDao(Works.class).queryBuilder();
            builder.where().in("id", workIds);
            builder.orderBy("show_order", true);

            List<Works> reviews = helper.getDao(Works.class).query(builder.prepare());
            return reviews;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
